#include "main.h"


HANDLE CList_AddMapFilter(HANDLE hContact, char* mapfilter) {
	DBWriteContactSettingString(hContact, PROTONAME, MAPFILTER, mapfilter);
	return hContact;
}


HANDLE CList_AddContact(const char* ip, const char* port, const char* game, const char* nick) {
	HANDLE hContact;
	hContact = (HANDLE)CallService(MS_DB_CONTACT_ADD, 0, 0);
	CallService(MS_PROTO_ADDTOCONTACT, (WPARAM)hContact, (LPARAM)PROTONAME);
	DBWriteContactSettingString(hContact, PROTONAME, ADDRESS, ip);
	DBWriteContactSettingString(hContact, PROTONAME, PORT, port);
	DBWriteContactSettingString(hContact, PROTONAME, GAMETYPE, game);
	DBWriteContactSettingString(hContact, PROTONAME, MAP, "map");
	DBWriteContactSettingString(hContact, PROTONAME, MAPFILTER, "");
	DBWriteContactSettingString(hContact, PROTONAME, PLAYERFILTER, "");
	DBWriteContactSettingString(hContact, PROTONAME, NICK, nick);
	return hContact;
}


int CList_ForEachContact(void callback(HANDLE)) {
	int numContacts = 0;
	HANDLE hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, 0);
	while(hContact != NULL) {
		if (!lstrcmp(PROTONAME, (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0))) {
			numContacts++;
			callback(hContact);
		}
		hContact=(HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM)hContact, 0);
	}
	return numContacts;
}


void CList_Update(HANDLE hContact) {
	char ip[MAX_IP];
	char port[MAX_PORT];
	char game[MAX_GAME];
	char lastMap[MAX_MAP];
	char nick[MAX_NICK];
	char mapfilter[MAX_MAPFILTER];
	DBVARIANT dbv;
	DBGetContactSetting(hContact, PROTONAME, ADDRESS, &dbv);
	strcpy(ip, dbv.pszVal);
	DBGetContactSetting(hContact, PROTONAME, PORT, &dbv);
	strcpy(port, dbv.pszVal);
	DBGetContactSetting(hContact, PROTONAME, GAMETYPE, &dbv);
	strcpy(game, dbv.pszVal);
	DBGetContactSetting(hContact, "CList", "MyHandle", &dbv);
	strcpy(nick, dbv.pszVal);
	DBGetContactSetting(hContact, PROTONAME, MAPFILTER, &dbv);
	strcpy(mapfilter, dbv.pszVal);
	DBGetContactSetting(hContact, PROTONAME, MAP, &dbv);
	strcpy(lastMap, dbv.pszVal);
	DBFreeVariant(&dbv);

	GS_SERVERINFO *info;
	info = GSQueryServer(gameList[gameIndex(game)].type, ip, atoi(port));
	if (info) {
		if (info->numplayers <= 0) {
			CList_SetAway(hContact);
		} else {
			CList_SetOnline(hContact);
			if (strcmp(lastMap, info->map) != 0) {
				if (Filter_CurrentMap(info->map, mapfilter)) {
					Notification_Map(hContact, nick, info);
				}
				DBWriteContactSettingString(hContact, PROTONAME, MAP, info->map);
				char conversion[4];
				DBWriteContactSettingString(hContact, PROTONAME, CLIENTS, itoa(info->numplayers, conversion, 10));
				DBWriteContactSettingString(hContact, PROTONAME, MAXCLIENTS, itoa(info->maxplayers, conversion, 10));
			}
			GSFreeServerInfo(info);
		}
	} else {
		CList_SetOffline(hContact);
	}
}


void CList_SetOnline(HANDLE hContact) {
	DBWriteContactSettingWord(hContact, PROTONAME, "Status", ID_STATUS_ONLINE);
}


void CList_SetOffline(HANDLE hContact) {
	DBWriteContactSettingWord(hContact, PROTONAME, "Status", ID_STATUS_OFFLINE);
	DBWriteContactSettingString(hContact, PROTONAME, MAP, "");
	DBWriteContactSettingString(hContact, PROTONAME, CLIENTS, "0");
}


void CList_SetAway(HANDLE hContact) {
	DBWriteContactSettingWord(hContact, PROTONAME, "Status", ID_STATUS_AWAY);
}


void CList_SetGlobalStatus(DWORD status, char* message) {
	DBVARIANT dbv;
	dbv.pszVal = message;
	BYTE x = DBGetContactSettingByte(NULL, "SRAway", StatusModeToDbSetting(status, "NoDlg"), 0);
	DBWriteContactSettingByte(NULL, "SRAway", StatusModeToDbSetting(status, "NoDlg"), 1);

	CallService(MS_CLIST_SETSTATUSMODE, status, 0);
	
	int count;
	PROTOCOLDESCRIPTOR **proto;

	CallService(MS_PROTO_ENUMPROTOCOLS, (WPARAM)&count, (LPARAM)&proto);
	for(int i = 0; i < count; i++) {
		CallProtoService(proto[i]->szName, PS_SETAWAYMSG, status, (LPARAM)message);
	}

	DBWriteContactSettingByte(NULL, "SRAway", StatusModeToDbSetting(status, "NoDlg"), x);
	DBFreeVariant(&dbv);
}
